import Http from "../http";

export async function industryApi(pid:number=0) {
    let url = 'industry/industry_sort/'
    if(pid > 0) {
        url += ('?pid=' + pid)
    }
    const { data } = await Http.get(url)
    const rows:any[] = []
    if(data) {
        data.forEach(row => {
            rows.push({
                label : row.name,
                id : row.id,
                value : row.code,
                isLeaf : pid > 0,
                ...row,
            })
        })
    }
    return rows
}



export async function industryTreeApi() {
    const { data } = await Http.get('industry/tree_industry_sort/')
    return data || []
}

export function industrySelectStockApi(ids:number[]) {
    return Http.post('industry/industry_codes/', {id : ids.join(',')})
}

export async function industryFactorApi() {
    const { data} = await Http.get('industry/factor_sort/')
    function reduce(rows, level=0) {
        rows.forEach((row, index) => {
            row.selectable = !row.children || row.children.length == 0
            row.key = `${row.id}_${index}_${level}`
            if(row.children && row.children.length > 0){
                reduce(row.children, level + 1)
            }
        })
    }

    if(data) {
        reduce(data)
    }

    return data || []
}

export function industryFactorDetailApi(params:any) {
    return Http.get('industry/factor_detail/', params)
}